package com.enwei.base;

import com.ew.action.R;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BaseActivity extends Activity{

	public ImageView toolback;
	public TextView pageTitle;
	
	private View mErrorView;
    private TextView mErrorText;
    private TextView mEmptyView;
    private TextView mLoadingView;
	private String title;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	public void setContentView(int resId,boolean isShowBack,boolean isShowTitle){
		setContentView(resId);
		initView(isShowBack,isShowTitle);
	}
	
	public void initView(boolean showBack,boolean showTitle){
		toolback = (ImageView) findViewById(R.id.goBack);
		pageTitle = (TextView) findViewById(R.id.pageTitle);
		
		mErrorView = findViewById(R.id.error_layout);
        if (mErrorView != null) {
            Button errorRetryBtn = (Button) mErrorView.findViewById(R.id.error_retry);
            mErrorText = (TextView) mErrorView.findViewById(R.id.error_text);
            errorRetryBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRetry();
                }
            });
        }

        mEmptyView = (TextView) findViewById(R.id.empty_text);

        mLoadingView = (TextView) findViewById(R.id.loading_text);
        if (mLoadingView != null) {
            final AnimationDrawable anim = (AnimationDrawable) mLoadingView.getCompoundDrawables()[1];
            mLoadingView.post(new Runnable() {
                @Override
                public void run() {
                    anim.start();
                }
            });
        }
		
		
		if(showBack && toolback!=null){
			toolback.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
		}else if(!showBack && toolback!=null){
			if(toolback !=null)toolback.setVisibility(View.GONE);
		}
		if(showTitle && pageTitle!=null){
			pageTitle.setText(getTitle());
		}
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	protected View getErrorLayout() {
        return mErrorView;
    }

    protected void setErrorText(String errorStr) {
        if (!TextUtils.isEmpty(errorStr)) {
            mErrorText.setText(errorStr);
        }
    }

    protected TextView getLoadingView() {
        return mLoadingView;
    }

    protected View getEmpty() {
        return mEmptyView;
    }

    protected void setEmptyText(String str) {
        mEmptyView.setText(str);
    }
	
	protected void onRetry() {
    }
	
}
